/**
 * 作者:hanbo
 * 日期:2018/11/1917:44
 * 描述:线绘制
 */
import {VectorBrush} from "./VectorBrush";
import {Line} from "../geometry/Line";
import {Polyline} from "../geometry/Polyline";

export class WidePolyLinesBrush extends VectorBrush {
    constructor(webGL, mapRender) {
        super(webGL);
        this.mapRender = mapRender;
    }


    /**
     * 批量绘制线
     * @param polyLines {Polyline[]} 缓冲区数组
     * @param index {Number} 图层索引
     * @param style {LineStyle|Undefined}
     */
    drawLines(polyLines, index, style) {
        this._polylines = polyLines;
        this.edgePointMount = [];
        this.pointMount = 0;
        for (let i = 0; i < this._polylines.length; i++) {
            this.edgePointMount[i] = this._polylines[i].path.length*2;//转面后每个点生成两个点
            this.pointMount = this.pointMount + this.edgePointMount[i];
        }
        this._verticesColors = new Float32Array(this.pointMount * 7);
        if (style) {
            this._width = style.width;
            for (let i = 0; i < this.pointMount; i++) {
                this._verticesColors[i * 7 + 2] = index;
                this._verticesColors[i * 7 + 3] = style.color.r;
                this._verticesColors[i * 7 + 4] = style.color.g;
                this._verticesColors[i * 7 + 5] = style.color.b;
                this._verticesColors[i * 7 + 6] = style.opacity;
            }
        }
        else {
            this._width = [];
            let lineStyle;
            //先将不变的深度值和颜色存储
            let currentIndex = 2;
            let polyline;
            for (let i = 0; i < this._polylines.length; i++) {
                polyline = this._polylines[i];
                lineStyle = polyline.getStyle();
                this._width.push(lineStyle.width);
                for (let j = 0; j < polyline.path.length*2; j++) {
                    this._verticesColors[currentIndex] = index;
                    currentIndex++;
                    this._verticesColors[currentIndex] = lineStyle.color.r;
                    currentIndex++;
                    this._verticesColors[currentIndex] = lineStyle.color.g;
                    currentIndex++;
                    this._verticesColors[currentIndex] = lineStyle.color.b;
                    currentIndex++;
                    this._verticesColors[currentIndex] = lineStyle.opacity;
                    currentIndex += 3;
                }
            }
        }
    }

    draw(projection) {
        super.draw(projection);
        this.getPixelWidth();
        this.flushData(this._verticesColors);
        this._compile();
    }

    /**
     * 重新获取每个像素对应的宽度
     */
    getPixelWidth() {
        if (this.pixelWidth) {
            let unit = this.mapRender.getUnitPerPixel();
            if (this.pixelWidth !== unit) {//比例尺变化
                this.pixelWidth = unit;
                this.createWideLine()
            }
        } else {//第一次绘制
            this.pixelWidth = this.mapRender.getUnitPerPixel();
            this.createWideLine()
        }
    }

    /**
     * 生成数据
     */
    createWideLine() {
        let path;
        let currentIndex=0;
        if (!Array.isArray(this._width)) {//宽度相同计算
            for (let i = 0; i < this._polylines.length; i++) {
                path =Polyline.toArea(this._polylines[i]._glPath, this._width * this.pixelWidth);
                for (let j = 0; j < path.length; j++) {
                    console.log( path[j][0]);
                    this._verticesColors[currentIndex] = path[j][0];
                    this._verticesColors[currentIndex + 1] = path[j][1];
                    currentIndex+=7;
                }
            }
        } else {
            for (let i = 0; i < this._polylines.length; i++) {//宽度不同计算
                path = Polyline.toArea(this._polylines[i]._glPath, this._width[i] * this.pixelWidth);
                for (let j = 0; j < path.length; j++) {
                    this._verticesColors[currentIndex] = path[j][0];
                    this._verticesColors[currentIndex + 1] = path[j][1];
                    currentIndex+=7;
                }
            }
        }

    }

    /**
     * 批量绘制线
     */
    _compile() {
        let currentIndex = 0;
        for (let i = 0; i < this.edgePointMount.length; i++) {
            this._webGL.drawArrays(this._webGL.TRIANGLE_STRIP, currentIndex, this.edgePointMount[i]);
            currentIndex=currentIndex+this.edgePointMount[i];
        }
    }

    clear() {
        super.clear();
        this._webGL.disableVertexAttribArray(this.a_Color);
        this._webGL.disableVertexAttribArray(this.a_Position);
    }
}